<script setup>
// TODO: 编辑
import { ref, reactive } from 'vue'
import axios from 'axios';
// 弹框开关
const dialogVisible = ref(false)
const form = reactive({
  name: '',
  place: "",
  id: ''
})
const emit = defineEmits(['on-update'])
const open = (row) => {
  //解构
  const { name, place, id } = row
  //赋值
  form.name = name
  form.place = place
  form.id = id
  //显示弹出框
  dialogVisible.value = true
}
defineExpose({ open })
//更新
const onUpdate = async () => {
  //收集表单数据 调用接口
  await axios.patch(`/edit/${form.id}`, {
    name: form.name,
    place: form.place,
  })
  //关闭弹框
  dialogVisible.value = false
  //更新父组件更新列表
  emit('on-update')
}
</script>

<template>
  <el-dialog v-model="dialogVisible" title="编辑" width="400px">
    <el-form label-width="50px">
      <el-form-item label="姓名">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="籍贯">
        <el-input v-model="form.place" placeholder="请输入籍贯" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onUpdate">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.el-input {
  width: 290px;
}
</style>
